package com.qa.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qa.common.ServerResponse;
import com.qa.dao.DownloadMapper;
import com.qa.pojo.Download;
import com.qa.pojo.User;
import com.qa.service.IDownloadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author
 * @Date 2020/3/11 22:22
 * @Desc
 **/
@Service
public class DownloadServiceImpl implements IDownloadService {
    @Autowired
    private DownloadMapper downloadMapper;

    @Override
    public PageInfo<Download> getAll(String downloadName,Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Download> downloads = downloadMapper.selectAllByStatus(downloadName);
        return new PageInfo<>(downloads);
    }

    @Override
    public List<Download> getByEmail(String email) {
        return downloadMapper.getByEmail(email);
    }

    @Override
    public ServerResponse upload(Download download, User user) {
        download.setAuthor(user.getName());
        download.setEmail(user.getEmail());
        download.setStatus(1);

        download.setHeadImg(user.getHeadImg());
        int insert = downloadMapper.insert(download);
        if (insert > 0) {
            return ServerResponse.createBySuccessMessage("成功！");
        }

        return ServerResponse.createByErrorMessage("失败！");
    }

    @Override
    public Download getById(Integer downloadId) {
        return downloadMapper.selectByPrimaryKey(downloadId);
    }

    @Override
    public ServerResponse deleteById(Integer downloadId) {
        int insert = downloadMapper.deleteByPrimaryKey(downloadId);
        if (insert > 0) {
            return ServerResponse.createBySuccessMessage("成功！");
        }

        return ServerResponse.createByErrorMessage("失败！");
    }

    @Override
    public int getCount(String beginTime, String endTime) {
        return downloadMapper.getCount(beginTime, endTime);
    }

    @Override
    public List<Download> getAllDownload(String keyword) {
        return downloadMapper.getAllDownload(keyword);
    }
}
